package com.example.mybookaidl.provider

import android.app.Activity
import android.content.ContentValues
import android.net.Uri
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.example.mybookaidl.R
import com.example.mybookaidl.aidl.Book

class ProviderKotlinActivity : AppCompatActivity(){

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_provider)
        // Uri uri = Uri.parse("content://com.ryg.chapter_2.book.provider");
        // getContentResolver().query(uri, null, null, null, null);
        // getContentResolver().query(uri, null, null, null, null);
        // getContentResolver().query(uri, null, null, null, null);
        val bookUri = Uri.parse("content://com.example.mybookaidl.provider/book")
        val values = ContentValues()
        values.put("_id", 6)
        values.put("name", "程序设计的艺术")
        contentResolver.insert(bookUri, values)
        val bookCursor = contentResolver.query(bookUri, arrayOf("_id", "name"), null, null, null)
        while (bookCursor!!.moveToNext()) {
            val book = Book()
            book.bookId = bookCursor.getInt(0)
            book.bookName = bookCursor.getString(1)
            Log.d(TAG, "query book:$book")
        }
        bookCursor.close()
        val userUri = Uri.parse("content://com.example.mybookaidl.provider/user")
        val userCursor = contentResolver.query(userUri, arrayOf("_id", "name", "sex"), null, null, null)
        while (userCursor!!.moveToNext()) {
            val user = User()
            user.apply {
                userId=userCursor.getInt(0)
                userName = userCursor.getString(1)
                isMale = userCursor.getInt(2) == 1
            }
//            user.userId = userCursor.getInt(0)
//            user.userName = userCursor.getString(1)
//            user.isMale = userCursor.getInt(2) == 1
            Log.d(TAG, "query user:$user")
        }
        userCursor.close()
    }

    companion object {
        private const val TAG = "ProviderActivity"
    }
}